//
//  NewlogTableViewController.m
//  shuilijuOASystem
//
//  Created by michael on 16/8/10.
//  Copyright © 2016年 czd. All rights reserved.
//

#import "NewlogTableViewController.h"
#import "OAUnDoneListCell.h"
#import "GDataDefines.h"
#import "selectview.h"
#import "MHDatePicker.h"
@interface NewlogTableViewController ()
@property (strong, nonatomic) MHDatePicker *selectDatePicker;

@end

@implementation NewlogTableViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    self.title = @"详情";
    self.tableView.bounces = NO;
  self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
      NSDate *currentDate = [NSDate date];//获取当前时间，日期
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-MM-dd "];
_dateString = [dateFormatter stringFromDate:currentDate];


 UIBarButtonItem * rightIem = [[UIBarButtonItem alloc] initWithTitle:@"发布" style:UIBarButtonItemStylePlain target:self action:@selector(searchAction)];
        self.navigationItem.rightBarButtonItem = rightIem;
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(keybordDown)];
    [self.tableView addGestureRecognizer:tap];

    
 }
- (void)searchAction{
    [self.view endEditing:YES];

 NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
 NSString *json =[NSString stringWithFormat: @"{'newsAuthor':'%@','newsContent':'%@', 'postTime':'%@'}",userId,_workplaceField.text,_dateString];

    [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];

    NSArray * array = @[@{@"json":json},@{@"userId":userId}];
           [HttpTools postWithURL:exchdialylog
     arrays:array method:@"CreateDaily" success:^(id JSON) {
             [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];

     NSData * data = [JSON dataUsingEncoding:NSUTF8StringEncoding];
       GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData: data options:0 error:nil];
         GDataXMLElement *rootElement = [doc rootElement];
    
     NSArray *infoData= [rootElement elementsForName:@"info"];
    for (int i = 0;i<[infoData count];i++)
    {
        GDataXMLElement *infoElement = [infoData objectAtIndex:i];
        self.infoString = [infoElement stringValue];
    }


     if ([_infoString isEqualToString:@"11"]) {
     [self.view makeToast:@"发送成功!"];
     [self .navigationController popViewControllerAnimated:YES];
}else{
 [self.view makeToast:@"发送失败!"];
      }
      
    } failure:^(NSError *error) {
            [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];

        
    }];
 



}
- (void)keybordDown {
 
    [self.view endEditing:YES];
        }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  if (indexPath.row==0) {
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];

     UITableViewCell* cell = [[UITableViewCell alloc]init];
     cell.selectionStyle = UITableViewCellSelectionStyleNone;
      UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, cell.contentView.height)];
    label.text = @"发布姓名 :";
    label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
      _nameField = [[UITextView alloc]init];
      _nameField.frame = CGRectMake(label.right, label.top+10, KScreenWidth-120, cell.height-10);
      _nameField.text = userId;
      UIColor *myColor = [UIColor colorWithWhite:0.5 alpha:0.2];
      _nameField.backgroundColor = myColor;
 [_nameField setFont:[UIFont systemFontOfSize:13]];
 _nameField.textAlignment = NSTextAlignmentLeft;
      _nameField.delegate = self;
       [cell.contentView addSubview:_nameField];
             return cell;

}


else if (indexPath.row==1) {

static NSString *cellid6 = @"cell6";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellid6];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid6];
             cell.selectionStyle = UITableViewCellSelectionStyleNone;
      UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, cell.contentView.height)];
    label.text = @"发布时间 :";
    
    label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
//      _timeField = [[UITextView alloc]init];
//      _timeField.frame = CGRectMake(label.right, label.top+10, KScreenWidth-120, cell.height-10);
//      _timeField.text = _dateString;
//      UIColor *myColor = [UIColor colorWithWhite:0.5 alpha:0.2];
//      _timeField.backgroundColor = myColor;
// [_timeField setFont:[UIFont systemFontOfSize:13]];
// _timeField.textAlignment = NSTextAlignmentLeft;
//      _timeField.delegate = self;
//       [cell.contentView addSubview:_timeField ];
    
    _databutton = [UIButton buttonWithType:UIButtonTypeCustom];
      _databutton.frame = CGRectMake(label.right, label.top, 120, label.height);
//    _button1.backgroundColor = [UIColor yellowColor];
    _databutton.titleLabel.font = [UIFont systemFontOfSize: 13.0];
    
                 [_databutton setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
           [_databutton addTarget:self action:@selector(dataaction) forControlEvents:UIControlEventTouchUpInside];

 
       [cell.contentView addSubview:_databutton];
}
             [_databutton setTitle:_dateString forState:UIControlStateNormal];

             return cell;

}
else  {

     UITableViewCell* cell = [[UITableViewCell alloc]init];
     cell.selectionStyle = UITableViewCellSelectionStyleNone;
      UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, 30)];
    label.text = @"内容 :";
    
    label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
      _workplaceField = [[UITextView alloc]init];
      _workplaceField.frame = CGRectMake(20, label.bottom+10, KScreenWidth-40, 100);
//      _nameField.text = @"333";
      UIColor *myColor = [UIColor colorWithWhite:0.5 alpha:0.2];
      _workplaceField.backgroundColor = myColor;
 [_workplaceField setFont:[UIFont systemFontOfSize:13]];
 _workplaceField.textAlignment = NSTextAlignmentLeft;
      _workplaceField.delegate = self;
       [cell.contentView addSubview:_workplaceField];
             return cell;

}

}


- (void)dataaction{
 _selectDatePicker = [[MHDatePicker alloc] init];
    _selectDatePicker.isBeforeTime = YES;
    _selectDatePicker.datePickerMode = UIDatePickerModeDate;
    
    __weak typeof(self) weakSelf = self;
    [_selectDatePicker didFinishSelectedDate:^(NSDate *selectedDate) {
      NSString *string = [weakSelf dateStringWithDate:selectedDate DateFormat:@"yyyy年MM月dd日"];
            _dateString = [string stringByReplacingOccurrencesOfString:@"年" withString:@"-"];
_dateString = [_dateString stringByReplacingOccurrencesOfString:@"月" withString:@"-"];
_dateString = [_dateString stringByReplacingOccurrencesOfString:@"日" withString:@""];
      
      
[self.tableView reloadData];
      [weakSelf.databutton setTitle:string forState:UIControlStateNormal];
    }];

}
- (NSString *)dateStringWithDate:(NSDate *)date DateFormat:(NSString *)dateFormat
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:dateFormat];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
    NSString *str = [dateFormatter stringFromDate:date];
    return str ? str : @"";
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 2) {
    return 140;
}
else{
return 40;
}
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
